package com.fjph.myapplication.ui.adapter

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.TextView

import com.bumptech.glide.Glide
import com.fjph.myapplication.R
import com.fjph.myapplication.model.StoriesBean

class MyAdapter(private val context: Context, private val layoutId: Int, private val list: List<StoriesBean>) : BaseAdapter() {

    override fun getCount(): Int {
        return list!!.size
    }

    override fun getItem(position: Int): Any {
        return list!![position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        var convertView = convertView

        val viewHolder: ViewHolder
        if (convertView == null) {
            viewHolder = ViewHolder()
            convertView = View.inflate(context, R.layout.zhihu_item, null)

            viewHolder.img = convertView!!.findViewById<View>(R.id.img) as ImageView
            viewHolder.title = convertView.findViewById<View>(R.id.txt_title) as TextView

            convertView.tag = viewHolder
        } else {
            viewHolder = convertView.tag as ViewHolder
        }
        viewHolder.title!!.text = list!![position].title


        Glide.with(context)
                .load(list!![position].images!![0])
                .into(viewHolder.img!!)


        return convertView

    }


    internal inner class ViewHolder {
        var img: ImageView? = null
        var title: TextView? = null
    }


}
